/*
 * Copyright (c) Ulrich Hilger, http://dev.uhilger.de, all rights reserved.
 *
 * Published under the terms and conditions of the Apache License 2.0,
 * see http://www.apache.org/licenses/LICENSE-2.0
 */

package de.uhilger.servicehub.ctrl.action;

import de.uhilger.app.ui.MainFrame;
import de.uhilger.servicehub.ServiceHub;
import de.uhilger.servicehub.view.PrefsDialog;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.prefs.Preferences;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;

/**
 * An action to select user preferences for application ServiceHub
 *
 * @author Ulrich Hilger, <a href="http://dev.uhilger.de" target="_blank">http://dev.uhilger.de</a>
 * @author Published under the terms and conditions of
 * the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License 2.0</a>
 */
public class PrefsAction extends AbstractAction {

  private ServiceHub owner;

  /**
   * Create a new object of class StartServerAction
   * @param owner the owner of this action
   */
  public PrefsAction(ServiceHub owner) {
    super();
    this.owner = owner;
    putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_E, KeyEvent.CTRL_MASK));
  }

  /**
   * Start the server, create a default server base directory
   * implicitly as required
   *
   * @param e the event that fired this action
   */
  public void actionPerformed(ActionEvent e) {
    try {
      MainFrame mf = ServiceHub.findMainFrame();
      PrefsDialog dlg = new PrefsDialog(mf, owner.getBundle());
      dlg.pack();
      dlg.setLocationRelativeTo(mf);
      dlg.setModal(true);
      dlg.setVisible(true);
      Preferences prefs = Preferences.userNodeForPackage(ServiceHub.class);
      boolean enterOwnLogin = prefs.getBoolean(ServiceHub.PREF_DEFAULT_DB_LOGIN, false);
      owner.setDbLoginEnabled(enterOwnLogin);
    } catch (Exception ex) {
      ServiceHub.logStackTrace(ex, getClass());
    }
  }

}
